<?php

require_once('classes/modeles/Structure/Affectation.php');
require_once('classes/modeles/Structure/Centre.php');
require_once('classes/modeles/Structure/Groupement.php');

/**
 * Description of Structure
 *
 * @author ROCU Fabien
 */
class Structure {

    public $array_affectation_centre;
    public $array_affectation_pompier;

    private $array_groupement_id;
    private $array_groupement;

    public $array_centre_id;
    public $array_centre;

    // Affectation

    function __construct() {

    }

    /**************************************************************************/
    /************************* AFFECTATION ************************************/
    /**************************************************************************/

    public function charger_affectation_centre ( $centre_id ) {
        global $_mysql;

        try {
            $query = "SELECT pompier_id FROM affectation WHERE centre_id = $centre_id";
            if ( Config::$debugSQL ) echo "<br />" . $query . "<br />";
            $result = $_mysql->query($query);

            if ( $_mysql->num_rows($result) > 0 ) {
                for ( $i=0 ; $val = $_mysql->fetch_array($result) ; $i++ ) {
                    $this->array_affectation_centre[$i] = $val['pompier_id'];
                }
            }
            else
                throw new Exception('Erreur Structure: problème rencontré lors du chargement des affectations d\'un centre');

        }
        catch (SQLException $sqle) {
            throw $sqle;
        }
    }
    
    public function charger_affectation_pompier ( $pompier_id ) {
        global $_mysql;

        try {
            $query = "SELECT centre_id FROM affectation WHERE pompier_id = $pompier_id";
            if ( Config::$debugSQL ) echo "<br />" . $query . "<br />";
            $result = $_mysql->query($query);

            if ( $_mysql->num_rows($result) > 0 ) {
                for ( $i=0 ; $val = $_mysql->fetch_array($result) ; $i++ ) {
                    $this->array_affectation_pompier[$i] = $val['centre_id'];
                }
            }
            else
                throw new Exception('Erreur Structure: problème rencontré lors du chargement des affectations d\'un pompier');

        }
        catch (SQLException $sqle) {
            throw $sqle;
        }
    }

    public function creer_affectation( $centre_id, $pompier_id ) {
        $affectation = new Affectation();
        $affectation->set_affectation($centre_id, $pompier_id);

        $id = $affectation->creer_affectation();
    }

    public function effacer_affectation( $centre_id, $pompier_id ) {
        $affectation = new Affectation();
        $affectation->effacer_affectation($centre_id, $pompier_id);
    }

    /**************************************************************************/
    /****************************** CENTRE ************************************/
    /**************************************************************************/

    public function charger_arrayCentreID_structure() {
        global $_mysql;

        $tmp = array();

        try {
            $query = "SELECT centre_id FROM centre";
            if ( Config::$debugSQL ) echo $query;
            $result = $_mysql->query($query);

            if ( $_mysql->num_rows($result) > 0 ) {
                for ($i = 0 ; $row = $_mysql->fetch_array($result) ; $i++) {
                    $tmp[$i] = $row['centre_id'];

                }

                $this->array_centre_id = $tmp;
            }
            else
                throw new Exception('Erreur Structure: la création de la liste des identifiants de centre a échoué');

        }
        catch (SQLException $sqle) {
            throw $sqle;
        }
    }

    public function charger_arrayCentre_structure() {
        $tmp = array();

        for ($i = 0 ; $i < count($this->array_centre_id) ; $i++) {
            $centre = new Centre();
            $centre->charger_centre($this->array_centre_id[$i]);

            $tmp[$this->array_centre_id[$i]] = serialize($centre);
        }

        $this->array_centre = $tmp;
    }

    public function creer_centre( $groupement_id, $nom ) {
        $centre = new Centre();
        $centre->set_centre($groupement_id, $nom);
        $id = $centre->creer_centre();

        $nb_centre = count($this->array_centre_id);
        $this->array_centre_id[$nb_centre] = $id;

        $this->array_centre[$id] = serialize($centre);
    }

    public function sauvegarder_centre( $id, $groupement_id, $nom ) {
        $centre = unserialize($this->array_centre[$id]);
        $centre->set_centre($groupement_id, $nom);
        $centre->sauvegarder_centre();

        $this->array_centre[$id] = serialize($centre);
    }

    public function effacer_centre( $id ) {
        $centre = new Centre();
        $centre->effacer_centre($id);

        unset($this->array_centre[$id]);

        $this->charger_arrayCentreID_structure();
    }


}

?>
